package com.east.entity;

import java.io.Serializable;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;

import javax.swing.*;

/**
 * 用户表
 *
 * @TableName t_user
 */
@Data
public class User implements Serializable, UserDetails {
    /**
     * 主键，自动增长，用户ID
     */
    private Integer id;

    /**
     * 登录账号
     */
    private String loginAct;

    /**
     * 登录密码
     */
    private String loginPwd;

    /**
     * 用户姓名
     */
    private String name;

    /**
     * 用户手机
     */
    private String phone;

    /**
     * 用户邮箱
     */

    private String email;

    /**
     * 账户是否没有过期，0已过期 1正常
     */
    private Integer accountNoExpired;

    /**
     * 密码是否没有过期，0已过期 1正常
     */
    private Integer credentialsNoExpired;

    /**
     * 账号是否没有锁定，0已锁定 1正常
     */
    private Integer accountNoLocked;

    /**
     * 账号是否启用，0禁用 1启用
     */
    private Integer accountEnabled;

    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 创建人
     */
    private Integer createBy;

    /**
     * 编辑时间
     */
    private String editTime;

    /**
     * 编辑人
     */
    private Integer editBy;

    /**
     * 最近登录时间
     */
    private String lastLoginTime;

    private static final long serialVersionUID = 1L;
    /**
     * dashboard菜单 list
     * button 拥有的按钮权限
     */
    private List<Permission> menuList;
    private List<Permission> buttonList;

    //--------------------实现UserDetails接口的7个方法-springSecurity---------------------

    /**
     * 角色list springSecurity
     */
    private List<String> roleList;
    /**
     * 权限list springSecurity
     */
    private List<String> permissionList;


    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> authorityList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(roleList)) {
            roleList.forEach(role -> {
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(role);
                authorityList.add(simpleGrantedAuthority);
            });
        }
        if (!ObjectUtils.isEmpty(permissionList)) {
            permissionList.forEach(permission -> {
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
                authorityList.add(simpleGrantedAuthority);
            });
        }
        if (!ObjectUtils.isEmpty(buttonList)) {
            buttonList.stream().forEach(permission -> {
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission.getCode());
                authorityList.add(simpleGrantedAuthority);
            });
        }
        return authorityList;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return this.getLoginPwd();
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return this.getLoginAct();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return this.accountNoExpired == 1;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return this.accountNoLocked == 1;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return this.credentialsNoExpired == 1;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return this.accountEnabled == 1;
    }
}